package com.lw.edu.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lw.commonutil.result.Result;
import com.lw.commonutil.vo.CourseVo;
import com.lw.edu.entity.Course;
import com.lw.edu.entity.Teacher;
import com.lw.edu.entity.chapter.ChapterVo;
import com.lw.edu.entity.frontvo.CourseQueryVo;
import com.lw.edu.entity.frontvo.CourseWebVo;
import com.lw.edu.service.ChapterService;
import com.lw.edu.service.CourseService;
import com.lw.edu.service.TeacherService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/edu/course")
public class CourseApiController {
    @Autowired
    private CourseService courseService;
    @Autowired
    private TeacherService teacherService;
    @Autowired
    private ChapterService chapterService;

    //查询最新8条热门课程数据
    @GetMapping("/index")
    public Result index(){
        QueryWrapper<Course> courseWrapper=new QueryWrapper<>();
        courseWrapper.orderByDesc( "id" );
        courseWrapper.last( "limit 8" );
        List<Course> courseList = courseService.list( courseWrapper );
        QueryWrapper<Teacher> teacherWrapper=new QueryWrapper<>();
        teacherWrapper.last( "limit 4" );
        List<Teacher> teacherList = teacherService.list( teacherWrapper );
        Map<String,Object> map=new HashMap<>();
        map.put( "courseList",courseList );
        map.put( "teacherList",teacherList );
        return Result.ok( map );
    }
    //条件带分页查询课程列表
    @PostMapping("/getCourseList/{page}/{limit}")
    public Result getCourseList(@PathVariable Long page,
                                @PathVariable Long limit,
                                @RequestBody(required = false) CourseQueryVo courseQueryVo){
        Page<Course> coursePage = new Page<>( page,limit );
        Page<Course> pageList = courseService.getCourseList(coursePage,courseQueryVo);
        return Result.ok(pageList);
    }
    //课程详情
    @GetMapping("/getCourseInfo/{id}")
    public Result getCourseInfo(@PathVariable String id){
        //编写sql语句查询课程信息
        CourseWebVo courseWebVo = courseService.getBaseCourseInfo(id);
        //根据课程id查询章节和小节
        List<ChapterVo> list = chapterService.getChapterVideoBycourseId( id );
        Map<String,Object> map = new HashMap<>();
        map.put( "courseInfo",courseWebVo );
        map.put( "list",list );
        return Result.ok( map );
    }
    //rpc根据课程id查找课程信息
    @PostMapping("/inner/getCourseInfo/{courseId}")
    public CourseVo getCourse(@PathVariable("courseId") String courseId){
        Course course = courseService.getById( courseId );
        CourseVo courseVo = new CourseVo();
        BeanUtils.copyProperties( course,courseVo );
        return courseVo;
    }
}
